/**
 * Copyright &copy; com.nmf
 */
package com.nmf.pc.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nmf.pc.enums.RspStatus;
import com.nmf.pc.service.MD5PawService;
import com.nmf.pc.vo.MemberLoginCodeReq;
import com.nmf.pc.vo.MemberLoginReq;
import com.nmf.pc.vo.RestPswCodeReq;
import com.nmf.pc.vo.ReturnResult;
import com.nmf.site.biz.member.gen.entity.BizMember;
import com.nmf.site.biz.member.gen.service.BizMemberService;
import com.nmf.site.biz.sms.bx.service.SmsCodeService;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.modules.sys.service.SystemService;
import com.taobao.api.ApiException;

/**
 * 订单Controller
 * @author 北冥
 * @version 2018-02-10
 */
@Controller
@RequestMapping(value = "/pc/p")
public class PcLoginController extends BaseController {
	@Autowired
	BizMemberService bizMemberService;
	@Autowired
	SmsCodeService smsCodeService;
	
	@RequestMapping(value = {"mem/loginPsw"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult loginPsw(@Valid MemberLoginReq memberLoginReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		
		BizMember bizMember = new BizMember();
		bizMember.setTelNo(memberLoginReq.getTelNo());
		List<BizMember> blist = bizMemberService.findList(bizMember);
		
		if(blist.size()==1) {
			BizMember dbBizMember = blist.get(0);
			if(!SystemService.validatePassword(memberLoginReq.getPassword(), dbBizMember.getPassword())) {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("密码错误，请重新输入密码");
			}else {
			    // 加入账号禁用功能
				if (dbBizMember.getDelFlag().equals(BizMember.DEL_FLAG_DELETE)) {
					returnResult.setStatus(RspStatus.FAILED.value);
					returnResult.setMessage("改账号已被系统管理员禁用，如有疑问请联系系统管理员！");
				} else {
					returnResult.setData("token", MD5PawService.getMd5Psw(memberLoginReq.getTelNo()));
				}
			}
		}else {
			returnResult.setStatus(RspStatus.FAILED.value);
			returnResult.setMessage("该用户不存在，请注册");
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"mem/loginCode"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult login(@Valid MemberLoginCodeReq memberLoginCodeReq, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
		ReturnResult returnResult = new ReturnResult();
		
		
		BizMember bizMember = new BizMember();
		bizMember.setTelNo(memberLoginCodeReq.getTelNo());
		List<BizMember> blist = bizMemberService.findList(bizMember);
		
		if(blist.size()==1) {
			BizMember dbBizMember = blist.get(0);
			
			String dbCode = smsCodeService.getCode(memberLoginCodeReq.getTelNo());
			if(memberLoginCodeReq.getCode().toLowerCase().equals(dbCode)) {
				// 如果用户被禁用，则不能登录成功
				if (dbBizMember.getDelFlag().equals(BizMember.DEL_FLAG_DELETE)) {
					returnResult.setStatus(RspStatus.FAILED.value);
					returnResult.setMessage("该账号已被系统管理员禁用，如有疑问请联系系统管理员！");
				} else {
					returnResult.setData("token", MD5PawService.getMd5Psw(memberLoginCodeReq.getTelNo()));
				}
			}else {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("验证码错误，请重新获取");
			}
		}else {
			returnResult.setStatus(RspStatus.FAILED.value);
			returnResult.setMessage("该用户不存在，请注册");
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"mem/resetPsw"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult resetPsw(@Valid RestPswCodeReq restPswCodeReq, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
		ReturnResult returnResult = new ReturnResult();
		
		
		BizMember bizMember = new BizMember();
		bizMember.setTelNo(restPswCodeReq.getTelNo());
		List<BizMember> blist = bizMemberService.findList(bizMember);
		
		if(blist.size()==1) {
			BizMember dbBizMember = blist.get(0);
			
			String dbCode = smsCodeService.getCode(restPswCodeReq.getTelNo());
			if(restPswCodeReq.getCode().toLowerCase().equals(dbCode)) {
				// 如果用户被禁用，则不能登录成功
				if (dbBizMember.getDelFlag().equals(BizMember.DEL_FLAG_DELETE)) {
					returnResult.setStatus(RspStatus.FAILED.value);
					returnResult.setMessage("该账号已被系统管理员禁用，如有疑问请联系系统管理员！");
				} else {
					BizMember saveBizMember = new BizMember();
					String enPassword = SystemService.entryptPassword(restPswCodeReq.getPsw());
					saveBizMember.setPassword(enPassword);
					saveBizMember.setId(dbBizMember.getId());
					bizMemberService.save(saveBizMember);
					
					returnResult.setData("token", MD5PawService.getMd5Psw(restPswCodeReq.getTelNo()));
				}
			}else {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("验证码错误，请重新获取");
			}
		}else {
			returnResult.setStatus(RspStatus.FAILED.value);
			returnResult.setMessage("该用户不存在，请注册");
		}
		return returnResult;
	}
	
	@RequestMapping(value = {"mem/veryMobileCode"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult getMobileCode(@RequestParam(required=true) String telNo,@RequestParam(required=true) String veryCode, HttpServletRequest request, HttpServletResponse response) throws ApiException {
		ReturnResult returnResult = new ReturnResult();
		String code = smsCodeService.getCode(telNo);
		if(code==null) {
			returnResult.setStatus(RspStatus.FAILED.value);
			returnResult.setMessage("请重新发送验证码");
		}else {
			if(code.toLowerCase().equals(veryCode.trim().toLowerCase())) {
				
			}else {
				returnResult.setStatus(RspStatus.FAILED.value);
				returnResult.setMessage("验证码错误");
			}
		}
		return returnResult;
	}

}